﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

class Program
{
    private static readonly HttpClient httpClient = new HttpClient();
    private static readonly Regex phoneRegex = new Regex(@"\b((\(\d{2,3}\)|\d{3,4}-)?\d{7,8}|\+?\d{11})\b");

    static async Task Main(string[] args)
    {
        Console.WriteLine("请输入关键词：");
        string keyword = Console.ReadLine();

        // 模拟用户访问
        httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36");

        // 获取搜索结果中的真实链接
        var searchUrls = GetSearchUrls(keyword);

        foreach (var searchUrl in searchUrls)
        {
            Console.WriteLine($"正在访问: {searchUrl}");
            string htmlContent = await GetPageContent(searchUrl);
            var realUrls = ExtractUrls(htmlContent);

            // 对真实链接进行进一步爬取
            foreach (var url in realUrls)
            {
                string pageContent = await GetPageContent(url);
                var phones = ExtractPhoneNumbers(pageContent);
                foreach (var phone in phones)
                {
                    Console.WriteLine(phone);
                }
            }
        }
    }

    private static List<string> GetSearchUrls(string keyword)
    {
        string baseUrl = "https://www.baidu.com/s?wd=";
        List<string> urls = new List<string>();
        for (int i = 0; i < 50; i += 10)
        {
            urls.Add($"{baseUrl}{Uri.EscapeDataString(keyword)}&pn={i}");
        }
        return urls;
    }

    private static async Task<string> GetPageContent(string url)
    {
        try
        {
            var response = await httpClient.GetAsync(url);
            if (response.IsSuccessStatusCode)
            {
                return await response.Content.ReadAsStringAsync();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"请求失败：{ex.Message}");
        }
        return string.Empty;
    }

    private static List<string> ExtractUrls(string html)
    {
        var matches = Regex.Matches(html, @"http[s]?://www\.baidu\.com/link\?url=[^""]+");
        return matches.Select(m => m.Value).ToList();
    }

    private static List<string> ExtractPhoneNumbers(string html)
    {
        var matches = phoneRegex.Matches(html);
        return matches.Select(m => m.Value).Distinct().ToList();
    }
}
